import 'package:flutter/material.dart';
import '../models/song.dart';

class SongTile extends StatelessWidget {
  final Song song;
  final bool isCurrentSong;
  final bool isPlaying;
  final VoidCallback? onTap;
  final VoidCallback? onMorePressed;
  final bool showMoreButton;
  final bool showDuration;
  final bool showIndex;
  final int? index;

  const SongTile({
    super.key,
    required this.song,
    this.isCurrentSong = false,
    this.isPlaying = false,
    this.onTap,
    this.onMorePressed,
    this.showMoreButton = true,
    this.showDuration = true,
    this.showIndex = false,
    this.index,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(12),
        color: isCurrentSong 
            ? Theme.of(context).primaryColor.withOpacity(0.1) 
            : null,
      ),
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
        leading: _buildLeading(context),
        title: _buildTitle(context),
        subtitle: _buildSubtitle(context),
        trailing: _buildTrailing(context),
        onTap: onTap,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(12),
        ),
      ),
    );
  }

  Widget _buildLeading(BuildContext context) {
    return Container(
      width: 48,
      height: 48,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(8),
        gradient: LinearGradient(
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
          colors: isCurrentSong
              ? [
                  Theme.of(context).primaryColor,
                  Theme.of(context).primaryColor.withOpacity(0.7),
                ]
              : [
                  Colors.grey[300]!,
                  Colors.grey[400]!,
                ],
        ),
        boxShadow: [
          BoxShadow(
            color: (isCurrentSong 
                ? Theme.of(context).primaryColor 
                : Colors.grey).withOpacity(0.3),
            blurRadius: 8,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Stack(
        alignment: Alignment.center,
        children: [
          // 背景图标
          Icon(
            Icons.music_note,
            color: Colors.white,
            size: 24,
          ),
          // 播放指示器
          if (isCurrentSong && isPlaying)
            Container(
              width: 16,
              height: 16,
              decoration: BoxDecoration(
                color: Colors.white,
                shape: BoxShape.circle,
                boxShadow: [
                  BoxShadow(
                    color: Colors.black.withOpacity(0.2),
                    blurRadius: 4,
                    offset: const Offset(0, 1),
                  ),
                ],
              ),
              child: Icon(
                Icons.volume_up,
                color: Theme.of(context).primaryColor,
                size: 12,
              ),
            ),
          // 序号显示
          if (showIndex && index != null && !isCurrentSong)
            Text(
              '${index! + 1}',
              style: const TextStyle(
                color: Colors.white,
                fontWeight: FontWeight.bold,
                fontSize: 12,
              ),
            ),
        ],
      ),
    );
  }

  Widget _buildTitle(BuildContext context) {
    return AnimatedDefaultTextStyle(
      duration: const Duration(milliseconds: 200),
      style: TextStyle(
        fontSize: 16,
        fontWeight: isCurrentSong ? FontWeight.bold : FontWeight.w500,
        color: isCurrentSong 
            ? Theme.of(context).primaryColor 
            : Theme.of(context).textTheme.bodyLarge?.color,
        height: 1.2,
      ),
      child: Text(
        song.name,
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
      ),
    );
  }

  Widget _buildSubtitle(BuildContext context) {
    return AnimatedDefaultTextStyle(
      duration: const Duration(milliseconds: 200),
      style: TextStyle(
        fontSize: 13,
        color: isCurrentSong 
            ? Theme.of(context).primaryColor.withOpacity(0.7)
            : Theme.of(context).textTheme.bodyMedium?.color?.withOpacity(0.7),
        height: 1.2,
      ),
      child: Text(
        '${song.artist} • ${song.album}',
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
      ),
    );
  }

  Widget _buildTrailing(BuildContext context) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        // 时长显示
        if (showDuration)
          Text(
            song.duration,
            style: TextStyle(
              color: Theme.of(context).textTheme.bodySmall?.color,
              fontSize: 12,
            ),
          ),
        
        // 播放状态指示器
        if (isCurrentSong && isPlaying) ...[
          const SizedBox(width: 8),
          Icon(
            Icons.graphic_eq,
            color: Theme.of(context).primaryColor,
            size: 20,
          ),
        ],
        
        // 更多按钮
        if (showMoreButton) ...[
          const SizedBox(width: 8),
          SizedBox(
            width: 32,
            height: 32,
            child: IconButton(
              padding: EdgeInsets.zero,
              onPressed: onMorePressed,
              icon: Icon(
                Icons.more_vert,
                size: 18,
                color: Theme.of(context).textTheme.bodyMedium?.color?.withOpacity(0.6),
              ),
            ),
          ),
        ],
      ],
    );
  }
}

// 简化版本的歌曲条目
class SimpleSongTile extends StatelessWidget {
  final Song song;
  final bool isCurrentSong;
  final VoidCallback? onTap;

  const SimpleSongTile({
    super.key,
    required this.song,
    this.isCurrentSong = false,
    this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return SongTile(
      song: song,
      isCurrentSong: isCurrentSong,
      onTap: onTap,
      showMoreButton: false,
      showDuration: false,
    );
  }
} 